package com.youlai.boot.sportPlan.service.impl;

import com.youlai.boot.common.enums.WeekEnum;
import com.youlai.boot.common.model.WeekCalendarVO;
import com.youlai.boot.core.security.util.SecurityUtils;
import com.youlai.boot.energyPlan.model.form.UserEnergyPlanForm;
import com.youlai.boot.weekPlan.model.form.UserWeekPlanForm;
import com.youlai.boot.weekPlan.service.UserWeekPlanService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.youlai.boot.sportPlan.mapper.SportPlanMapper;
import com.youlai.boot.sportPlan.service.SportPlanService;
import com.youlai.boot.sportPlan.model.entity.SportPlan;
import com.youlai.boot.sportPlan.model.form.SportPlanForm;
import com.youlai.boot.sportPlan.model.query.SportPlanQuery;
import com.youlai.boot.sportPlan.model.vo.SportPlanVO;
import com.youlai.boot.sportPlan.converter.SportPlanConverter;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;

/**
 * 用户饮食计划服务实现类
 *
 * @author cuisheng
 * @since 2025-03-31 13:11
 */
@Service
@RequiredArgsConstructor
public class SportPlanServiceImpl extends ServiceImpl<SportPlanMapper, SportPlan> implements SportPlanService {

    private final SportPlanConverter sportPlanConverter;
    private final UserWeekPlanService userWeekPlanService;

    /**
    * 获取用户饮食计划分页列表
    *
    * @param queryParams 查询参数
    * @return {@link IPage<SportPlanVO>} 用户饮食计划分页列表
    */
    @Override
    public IPage<SportPlanVO> getSportPlanPage(SportPlanQuery queryParams) {
        Page<SportPlanVO> pageVO = this.baseMapper.getSportPlanPage(
                new Page<>(queryParams.getPageNum(), queryParams.getPageSize()),
                queryParams
        );
        return pageVO;
    }
    
    /**
     * 获取用户饮食计划表单数据
     *
     * @param id 用户饮食计划ID
     * @return
     */
    @Override
    public SportPlanForm getSportPlanFormData(Long id) {
        SportPlan entity = this.getById(id);
        return sportPlanConverter.toForm(entity);
    }
    
    /**
     * 新增用户饮食计划
     *
     * @param formData 用户饮食计划表单对象
     * @return
     */
    @Override
    public boolean saveSportPlan(SportPlanForm formData) {
        SportPlan entity = sportPlanConverter.toEntity(formData);
        return this.save(entity);
    }
    
    /**
     * 更新用户饮食计划
     *
     * @param id   用户饮食计划ID
     * @param formData 用户饮食计划表单对象
     * @return
     */
    @Override
    public boolean updateSportPlan(Long id,SportPlanForm formData) {
        SportPlan entity = sportPlanConverter.toEntity(formData);
        return this.updateById(entity);
    }
    
    /**
     * 删除用户饮食计划
     *
     * @param ids 用户饮食计划ID，多个以英文逗号(,)分割
     * @return
     */
    @Override
    public boolean deleteSportPlans(String ids) {
        Assert.isTrue(StrUtil.isNotBlank(ids), "删除的用户饮食计划数据为空");
        // 逻辑删除
        List<Long> idList = Arrays.stream(ids.split(","))
                .map(Long::parseLong)
                .toList();
        return this.removeByIds(idList);
    }

    @Override
    public List<WeekCalendarVO> weekCalendar(List<String> dates) {
        Long userId = SecurityUtils.getUserId();
        Map<Integer,String> dietPlanWeekCalendarVOMap=new HashMap<>();
        for (WeekEnum weekEnum : WeekEnum.values()) {
            dietPlanWeekCalendarVOMap.put(weekEnum.getValue(),weekEnum.getLabel());
        }

        List<WeekCalendarVO> weekCalendarVOS = baseMapper.weekCalendar(userId, dates);
        if (weekCalendarVOS != null&&!weekCalendarVOS.isEmpty()) {
            for (WeekCalendarVO vo : weekCalendarVOS) {
                vo.setWeek(dietPlanWeekCalendarVOMap.get(Integer.parseInt(vo.getWeek())));
                vo.setIsBeyond(false);
            }
        }
        return weekCalendarVOS;
    }

}
